package com.share.sharedevice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.sharedevice.entity.CabinetType;
import com.share.sharedevice.mapper.CabinetTypeMapper;
import com.share.sharedevice.service.ICabinetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CabinetTypeServiceImpl extends ServiceImpl<CabinetTypeMapper, CabinetType> implements ICabinetTypeService
{
    @Autowired
    private CabinetTypeMapper cabinetTypeMapper;

    /**
     * 查询柜机类型列表
     *
     * @param cabinetType 柜机类型
     * @return 柜机类型
     */
    @Override
    public List<CabinetType> selectCabinetTypeList(CabinetType cabinetType)
    {
        return cabinetTypeMapper.selectCabinetTypeList(cabinetType);
    }

    /**
     * 查询柜机类型
     *
     * @param id 柜机类型ID
     * @return 柜机类型
     */
    @Override
    public CabinetType selectCabinetTypeById(Long id) {

        CabinetType cabinetType = cabinetTypeMapper.selectCabinetTypeById(id);

        return cabinetType;
    }

    /**
     * 新增柜机类型
     *
     * @param cabinetType 柜机类型
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(CabinetType cabinetType) {

        String name = cabinetType.getName();

        if (name != null && name.length() > 0) {
            CabinetType cabinetType1 = cabinetTypeMapper.selectCabinetTypeByName(name);
            if (cabinetType1 != null) {
                throw new RuntimeException("名称已存在");
            }
        } else if (name == null || name.length() == 0){
            throw new RuntimeException("名称不能为空");
        }

        Integer totalSlots = cabinetType.getTotalSlots();

        if (totalSlots == null || totalSlots <= 0) {
            throw new RuntimeException("总插槽数量不能小于0");
        }

        cabinetType.setStatus("0");

        cabinetType.setDelFlag("0");

        cabinetType.setCreateTime(new Date());
        cabinetType.setUpdateTime(new Date());

        cabinetTypeMapper.add(cabinetType);
    }

    /**
     * 修改柜机类型
     *
     * @param cabinetType 柜机类型
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatedevice(CabinetType cabinetType) {

        String name = cabinetType.getName();
        if (name != null && name.length() > 0) {
            CabinetType cabinetType1 = cabinetTypeMapper.selectCabinetTypeByName(name);
            if (cabinetType1 != null && name.equals(cabinetType.getName())) {
                throw new RuntimeException("名称已存在");
            }
        } else if (name == null || name.length() == 0){
            throw new RuntimeException("名称不能为空");
        }

        cabinetType.setUpdateTime(new Date());

        cabinetTypeMapper.updatedevice(cabinetType);

    }

    /**
     * 删除柜机类型
     *
     * @param ids ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Long> ids) {
        cabinetTypeMapper.deleteByIds(ids);
    }

}